package cn.gy.productclient.controller;

import cn.gy.common.model.Product;
import cn.gy.productclient.client.ProductClient;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@AllArgsConstructor
@RestController
@Slf4j
public class ProductClientController {
    @Autowired
    private final ProductClient productClient;

    /**
     * 根据商品id查询商品
     */
    @GetMapping("/findByProductId/{productId}")

    public Product findByProductId(@PathVariable Long productId) {

        log.info("--- in findByProductId-----" + productId);
        Product product = productClient.findByProductId(productId);

        if (product != null) {
            log.info("--- findByProductId-----" + productId);
        } else {
            log.info("--- not Id----:"+productId);
        }
        return product;
    }
    /**
     * 根据商品名称查询商品
     */
    @GetMapping("/findByProductName/{nm}")
    public Product findByProductName(@PathVariable String nm) {
        Product product = productClient.findByProductName(nm);
        if (product != null) {
            log.info("-----find -----:" + nm);
        } else {
            log.info("-----not found -----:"+nm);
        }
        return product;
    }
    /**
    /**
     * 查询所有商品
     */
    @GetMapping("/queryAllProduct")
    public List<Product> queryAllProduct() {
        List<Product> productList = productClient.queryAllProduct();
        if (productList != null) {
            log.info("-----queryAllProduct-----:" + productList.size());
        } else {
            log.info("-----queryAllProduct------:0");
        }
        return productList;
    }

}
